const Controller = require('egg').Controller;

class HomeController extends Controller {

    /// 获取资讯列表数据
    async getNews() {
        const { ctx, app } = this
        const data = await ctx.service.news.getNews()
        if (typeof data == 'string') {
            ctx.body = {
                code: -1,
                message: data
            }
        } else {
            ctx.body = {
                code: 1,
                data
            }
        }
    }

    /// 添加资讯数据
    async addNews() {
        const { ctx, app } = this
        const data = await ctx.service.news.addNews()
        if (data.affectedRows === 1) {
            ctx.body = {
                code: 1,
                message: '添加成功'
            }
        } else {
            ctx.body = {
                code: -1,
                message: '添加失败'
            }
        }
    }

    /// 编辑资讯数据
    async editNews() {
        const { ctx, app } = this
        const data = await ctx.service.news.editNews()
        if (data.affectedRows === 1) {
            ctx.body = {
                code: 1,
                message: '编辑成功'
            }
        } else {
            ctx.body = {
                code: -1,
                message: '编辑失败'
            }
        }
    }

    /// 删除资讯数据
    async delNews() {
        const { ctx, app } = this;
        const data = await ctx.service.news.delNews()
        if (data.affectedRows === 1) {
            ctx.body = {
                code: 1,
                message: '删除成功'
            }
        } else {
            ctx.body = {
                code: -1,
                message: '删除失败'
            }
        }
    }
}

module.exports = HomeController;